package com.handong.handserialprogram.module.setVoice;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.module.setting.SettingActivity;
import com.handong.handserialprogram.utils.SPUtil;

public class HintActivity extends AppCompatActivity {
    private String TAG = "HintActivity.class";
    private SeekBar seekBar_jh, seekBar_bj, seekBar_ts;
    private TextView tvProgress_jh, tvProgress_bj, tvProgress_ts;
    private TextView headerCenterTitle, mBtnStarDownload;
    private ImageView headerImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hint);
        initUI();
        initData();
    }

    private void initUI() {
        mBtnStarDownload = findViewById(R.id.headerOptionItem);
        headerCenterTitle = findViewById(R.id.headerCenterTitle);
        seekBar_jh = findViewById(R.id.seek_jh);
        seekBar_bj = findViewById(R.id.seek_bj);
        seekBar_ts = findViewById(R.id.seek_ts);
        tvProgress_jh = findViewById(R.id.progress_jh);
        tvProgress_bj = findViewById(R.id.progress_bj);
        tvProgress_ts = findViewById(R.id.progress_ts);
        headerImage=findViewById(R.id.headerBackLayoutIcon);
        headerImage.setImageResource(R.drawable.ic_menu_back_144);

    }

    private void initData() {
        headerCenterTitle.setText("脚环音量设置");
        mBtnStarDownload.setVisibility(View.VISIBLE);
        findViewById(R.id.headerOptionLayout).setVisibility(View.VISIBLE);

        mBtnStarDownload.setText("确定");
        initVoice();

        seekBar_jh.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvProgress_jh.setText((progress) + "%");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        seekBar_bj.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvProgress_bj.setText((progress + 1) + "%");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        seekBar_ts.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvProgress_ts.setText((progress + 1) + "%");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        findViewById(R.id.headerBackLayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        findViewById(R.id.headerOptionItem).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveVoiceData();
                Intent setIntent = new Intent(HintActivity.this, SettingActivity.class);
                startActivity(setIntent);
                finish();
            }
        });
    }

    private void saveVoiceData() {
        int jh_voice = seekBar_jh.getProgress();
        SPUtil.putIntValue(SPUtil.JH_VOICE, jh_voice);
        Log.d("保存数据","狡猾音量"+jh_voice+"转："+(Integer.toHexString((int)Math.ceil(SPUtil.getIntValue(SPUtil.JH_VOICE) * 15 / 100f))));
        SPUtil.putBooleanValue(SPUtil.JH_OPEN, jh_voice == 0 ? false : true);

        int bj_voice = seekBar_bj.getProgress()+1;
        SPUtil.putIntValue(SPUtil.BJ_VOICE, bj_voice);

        int ts_voice = seekBar_ts.getProgress()+1;
        SPUtil.putIntValue(SPUtil.TS_VOICE, ts_voice);
//        Log.d(TAG, "保存3 原本：" + ts_voice + "转：" + (int)Math.ceil(ts_voice * 15 / 100f));

    }

    private void initVoice() {
        int jh_data = SPUtil.getIntValue(SPUtil.JH_VOICE);
        seekBar_jh.setProgress(jh_data);
        tvProgress_jh.setText((jh_data) + "%");

        int bj_data = SPUtil.getIntValue(SPUtil.BJ_VOICE);
        seekBar_bj.setProgress(bj_data-1);
        tvProgress_bj.setText((bj_data) + "%");

        int ts_data = SPUtil.getIntValue(SPUtil.TS_VOICE);
//        Log.d(TAG, "接受3 " + ts_data );
        seekBar_ts.setProgress(ts_data-1);
        tvProgress_ts.setText((ts_data) + "%");

    }
}
